package com.ruoyi.scrap.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.enums.DeleteStatus;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.scrap.domain.ScrapApplyInto;
import com.ruoyi.scrap.domain.ScrapApplyOut;
import com.ruoyi.scrap.domain.po.ScrapApplyIntoPo;
import com.ruoyi.scrap.mapper.ScrapApplyIntoMapper;
import com.ruoyi.scrap.mapper.ScrapApplyOutMapper;
import com.ruoyi.scrap.service.ScrapApplyIntoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ScrapApplyIntoServiceImpl extends ServiceImpl<ScrapApplyIntoMapper, ScrapApplyInto> implements ScrapApplyIntoService {

    @Resource
    ScrapApplyOutMapper outMapper;

    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<ScrapApplyIntoPo> getScrapApplyInto(ScrapApplyIntoPo into){
        return baseMapper.getScrapApplyInto(into);
    }

    @Override
    public ScrapApplyIntoPo getInfo(Long id) {

        ScrapApplyInto into = baseMapper.selectById(id);

        if(ObjectUtils.isEmpty(into)){
            throw new BaseException("查询失败：未查到入库单信息");
        }

        ScrapApplyIntoPo result = new ScrapApplyIntoPo();

        BeanUtils.copyProperties(into,result);

        List<ScrapApplyOut> outs = outMapper.selectList(Wrappers.lambdaQuery(ScrapApplyOut.class)
                .eq(ScrapApplyOut::getIntoId,id)
                .eq(ScrapApplyOut::getDelFlag, DeleteStatus.OK.getCode()));

        if(!ObjectUtils.isEmpty(outs)){

            BeanUtils.copyProperties(outs.get(0),result);

        }else{

            result.setWeightEnd(into.getIntoWeight());

        }

        result.setIntoId(id);

        return result;

    }
}
